﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

namespace Web
{
    public partial class DownLoadShuomingDocAndTv : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["filename"] == null)
            {
                Response.Write("<script>alert('操作有误，请重新点击下载');</script>");
            }
            else if (Request.QueryString["filename"].LastIndexOf('.')==-1)
            {
                Response.Write("<script>alert('没有您要的文件，请及时与系统管理员联系');</script>");  
            }
            else 
            {
                DownLoad(Request.QueryString["filename"].ToString());
            }
        }
        protected void DownLoad(string nameoFfile) 
        {
            try
            {
                string fileName = nameoFfile;//客户端保存的文件名
                string filePath = Server.MapPath("downLoad/" + nameoFfile);//路径

                FileInfo fileInfo = new FileInfo(filePath);
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
                Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                Response.AddHeader("Content-Transfer-Encoding", "binary");
                Response.ContentType = "application/octet-stream";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                Response.WriteFile(fileInfo.FullName);
                Response.Flush();
                Response.End();
            }
            catch (Exception ex )
            {
                Response.Write("<script>alert('未能找到您要的文件');</script>");
            }
        }
    }
}
